package pro.jiug.kExcel.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import pro.jiug.kExcel.entity.User;
import pro.jiug.kExcel.service.UserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * excel导出控制器
 *
 */
@Controller
public class ExportController {

    @Resource
    private UserService userService;

    /**
     * 导出excel
     */
    @RequestMapping(value = "exportExcel")
    public void exportExcel(HttpServletResponse response, String type, String start, String end) throws Exception {

        //查询结果
        PageInfo<User> page = userService.findPage(1,10, new User());

        List<User> userList = page.getList();

        ArrayList<String> titleList = new ArrayList<String>();
        ArrayList<List<String>> contentList = new ArrayList<>();

        String title="用户";

        titleList.add("序号");
        titleList.add("姓名");
        titleList.add("密码");

        //封装数据
        for (int i = 0; i < userList.size(); i++) {
            List<String> rowList=new ArrayList<>();
            rowList.add(userList.get(i).getId().toString());
            rowList.add(userList.get(i).getName());
            rowList.add(userList.get(i).getPassword());
            contentList.add(rowList);
        }

        //第一步，创建一个workbook对应一个excel文件
        XSSFWorkbook workbook = new XSSFWorkbook();
        //第二部，在workbook中创建一个sheet对应excel中的sheet
        XSSFSheet sheet = workbook.createSheet("统计分析");
        //第三部，在sheet表中添加表头第0行，老版本的poi对sheet的行列有限制
        XSSFRow row = sheet.createRow(0);

        //第四步，创建单元格，设置表头
        for (int i = 0; i < titleList.size(); i++) {
            XSSFCell cell = row.createCell(i);
            cell.setCellValue(titleList.get(i));
        }

        //第五步，写入数据
        int size = contentList.get(0).size();
        for (int i = 0; i < size; i++) {
            XSSFRow row1 = sheet.createRow(i + 1);
            for (int j = 0; j < contentList.get(i).size(); j++) {
                //创建单元格设值
                row1.createCell(j).setCellValue(String.valueOf(contentList.get(i).get(j)));
            }
        }

        BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
        title = URLEncoder.encode(title + "报表.xlsx", "UTF-8");
        // 客户使用保存文件的对话框：
        response.setHeader("Content-disposition", "inline;filename=" + title);
        // 通知客户文件的MIME类型：
        response.setContentType("application/octet-stream xlsx;charset=gb2312");
        workbook.write(bos);
        bos.flush();
        bos.close();
    }

}
